package com.realdolmen.whiteyellowcross.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Van Heddeghem Frederik
 * 
 */
public class DateTimeHelper {

	/**
	 * @param time
	 *            Time in string format you wish to create a date from.for
	 *            example 08:00
	 * @param timeFormat
	 *            format of time you wish to create a date from.for example
	 *            (HH:mm)
	 * @return date date Object string from 1 januari 1970 with correct time in
	 *         it.
	 */
	public static Date constructDateFromTime(String time, String timeFormat) {
		DateFormat dateTimeFormat = new SimpleDateFormat(timeFormat);

		Date date;
		try {
			date = dateTimeFormat.parse(time);
		} catch (ParseException e) {
			return Calendar.getInstance().getTime();
		}
		return date;
	}

	/**
	 * @param month
	 *            :month of year
	 * @param year
	 *            :some year
	 * @return total days of year and month
	 */
	public static int getDaysInMonth(int month, int year) {
		Calendar calendar = Calendar.getInstance();
		int date = 1;
		calendar.set(year, month, date);
		int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		return days;
	}

	/**
	 * @param day
	 *            :day of month(zero based)
	 * @param month
	 *            :month of year
	 * @param year
	 *            :some year
	 * @return java date object for a given day,month,year
	 */
	public static Date constructDateFromDayMonthYear(int day, int month,
			int year) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		return calendar.getTime();
	}

	/**
	 * @param date
	 *            :some date to format
	 * @param format
	 *            :format in which the date/time should be formatted
	 * @return formatted date/time in string format
	 */
	public static String formatDateTime(Date date, String format) {
		DateFormat dateTimeFormat = new SimpleDateFormat(format);
		return dateTimeFormat.format(date);
	}

}
